package o;

import android.app.job.JobScheduler;
import android.content.Context;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.HandlerThread;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.card.base.bean.BaseDistCardBean;
import com.huawei.appgallery.serverreqkit.api.bean.startup.StartupResponse$OOBEAppInfo;
import com.huawei.appmarket.oobe.app.OOBEFlowController;
import com.huawei.appmarket.oobe.store.OOBEQueryIfSupportTask;
import com.huawei.appmarket.oobe.store.OOBEQueryIfSupportTaskV2;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import o.dkb.d;
import o.dqw.c;

public final class dlm {
    private static final HandlerThread ˋ = new HandlerThread("OOBEAsyncHandler");
    private static final Handler ˎ = new Handler(ˋ.getLooper());

    static {
        ˋ.start();
    }

    public static void ˊ(Runnable runnable) {
        ˎ.post(runnable);
    }

    public static void ˊ() {
        Logger.ˎ("OOBE", "OOBEAsyncHandler.storeDownloadTask");
        ˊ(new Runnable() {
            public void run() {
                Context ˋ = ContentManager.getInstant().getContext();
                if (!dld.ˎ().ᐝ().isEmpty()) {
                    final List arrayList = new ArrayList();
                    List<StartupResponse$OOBEAppInfo> ͺ = dld.ˎ().ͺ();
                    if (!CollectCheck.isEmpty(ͺ)) {
                        Logger.ˎ("OOBE", "countDownLatch release. oobeAppInfos.size()=" + ͺ.size());
                        final CountDownLatch countDownLatch = new CountDownLatch(ͺ.size());
                        for (StartupResponse$OOBEAppInfo startupResponse$OOBEAppInfo : ͺ) {
                            if (!dli.ˊ(ˋ, startupResponse$OOBEAppInfo.ˎ())) {
                                dlm.ˊ(startupResponse$OOBEAppInfo, new d(this) {
                                    final /* synthetic */ AnonymousClass3 ˏ;

                                    public void ˋ(@Nullable SessionDownloadTask sessionDownloadTask) {
                                        if (sessionDownloadTask != null) {
                                            arrayList.add(sessionDownloadTask);
                                        }
                                        countDownLatch.countDown();
                                        Logger.ˎ("OOBE", "countDownLatch release. countDown");
                                    }
                                });
                            }
                        }
                        try {
                            countDownLatch.await();
                            Logger.ˎ("OOBE", "countDownLatch release. tasklist size=" + arrayList.size());
                        } catch (InterruptedException e) {
                            Logger.ˋ("OOBE", "InterruptedException");
                        } finally {
                            arrayList = "OOBE";
                            ˋ = "getDependDownloadTaskList end.";
                            Logger.ˎ(arrayList, ˋ);
                        }
                    }
                    dli.ˊ(ˋ).edit().putInt("changeID", dld.ˎ().ˊॱ()).putString("lastVNKey", dld.ˎ().ʿ()).commit();
                    try {
                        Thread.sleep(3500);
                    } catch (InterruptedException e2) {
                        Logger.e("OOBE", "handleActionStoreDownloadTask exception");
                    }
                    int ॱ = dvt.ॱ(ˋ);
                    int i = 4;
                    for (SessionDownloadTask sessionDownloadTask : r1) {
                        i |= sessionDownloadTask.ʽॱ();
                        if ((sessionDownloadTask.ʽॱ() & ॱ) > 0) {
                            dlm.ˎ(sessionDownloadTask, 1);
                            dvt.ˏ().ʽ(sessionDownloadTask);
                        } else {
                            dlm.ˎ(sessionDownloadTask, 2);
                            dvt.ˏ().ʻ(sessionDownloadTask);
                        }
                    }
                    dli.ˊ(ˋ).edit().putInt("userSelectNetType", i).commit();
                    Intent intent = new Intent("com.huawei.appmarket.oobe.app.action.STORE_DOWNLOAD_TASK_FINISHED");
                    intent.setClass(ˋ, OOBEFlowController.class);
                    ll.ˏ(ˋ).ˋ(intent);
                }
                dld.ˎ().ॱˎ();
            }
        });
    }

    private static void ˎ(SessionDownloadTask sessionDownloadTask, int i) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(RpkInfo.APPID, sessionDownloadTask.ॱˋ());
        linkedHashMap.put("type", String.valueOf(i));
        linkedHashMap.put("service_type", String.valueOf(ccz.ॱ()));
        linkedHashMap.put("detailid", sessionDownloadTask.ॱˊ());
        dli.ˏ("card_installbtn_click", linkedHashMap);
    }

    public static void ˎ(final int i) {
        Logger.ˎ("OOBE", "OOBEAsyncHandler.startQuerySupportState");
        ˊ(new Runnable() {
            public void run() {
                Context ˋ = ContentManager.getInstant().getContext();
                if (dli.ˋ()) {
                    new OOBEQueryIfSupportTaskV2(ˋ, i).ॱ();
                } else {
                    new OOBEQueryIfSupportTask(ˋ, i).ॱ();
                }
            }
        });
    }

    private static void ˊ(final StartupResponse$OOBEAppInfo startupResponse$OOBEAppInfo, final d dVar) {
        if (startupResponse$OOBEAppInfo != null) {
            BaseDistCardBean ˊ = dqw.ˊ(startupResponse$OOBEAppInfo.ˎ(), startupResponse$OOBEAppInfo.ˋॱ(), startupResponse$OOBEAppInfo.ॱ());
            dqw o_dqw = new dqw();
            o_dqw.ˎ(false);
            o_dqw.ˏ(ˊ, new c() {
                public void ˋ(SessionDownloadTask sessionDownloadTask) {
                    if (sessionDownloadTask != null) {
                        if (startupResponse$OOBEAppInfo.ˋॱ() == 0) {
                            SplitTask splitTask = new SplitTask();
                            splitTask.ˋ(startupResponse$OOBEAppInfo.ᐝ());
                            splitTask.ˊ(startupResponse$OOBEAppInfo.ʻ());
                            splitTask.ˊॱ(startupResponse$OOBEAppInfo.ˎ());
                            try {
                                splitTask.ˋ(Long.parseLong(startupResponse$OOBEAppInfo.ʽ()));
                            } catch (NumberFormatException e) {
                                Logger.e("OOBE", "OOBEAsyncHandler setSize_ NumberFormatException=" + e.getMessage());
                            }
                            sessionDownloadTask.ˏ(splitTask);
                        }
                        sessionDownloadTask.ʼ(startupResponse$OOBEAppInfo.ˎ());
                        sessionDownloadTask.ˊ(startupResponse$OOBEAppInfo.ᐝ());
                        sessionDownloadTask.ॱ(startupResponse$OOBEAppInfo.ˋ());
                        sessionDownloadTask.ॱॱ(startupResponse$OOBEAppInfo.ˏ());
                        sessionDownloadTask.ˏ(startupResponse$OOBEAppInfo.ˊ());
                        sessionDownloadTask.ʽ(startupResponse$OOBEAppInfo.ʼ());
                        sessionDownloadTask.ˋॱ(4);
                        sessionDownloadTask.ᐝ(dld.ˎ().ˏॱ());
                        sessionDownloadTask.ʼ(false);
                        sessionDownloadTask.ॱˊ(startupResponse$OOBEAppInfo.ˋॱ());
                        try {
                            sessionDownloadTask.ʻ(Integer.parseInt(startupResponse$OOBEAppInfo.ॱ()));
                        } catch (NumberFormatException e2) {
                            Logger.e("OOBE", "OOBEAsyncHandler setVersionCode NumberFormatException=" + e2.getMessage());
                        }
                    }
                    if (dVar != null) {
                        dVar.ˋ(sessionDownloadTask);
                    }
                }

                public void ॱ() {
                    Logger.e("OOBE", "castToDownloadTask get bundle info fail.");
                    if (dVar != null) {
                        dVar.ˋ(null);
                    }
                }
            });
        }
    }

    @RequiresApi(api = 21)
    public static void ˎ() {
        Logger.ˎ("OOBE", "OOBEAsyncHandler.startDownload");
        ˊ(new Runnable() {
            public void run() {
                Context ˋ = ContentManager.getInstant().getContext();
                List<SessionDownloadTask> ॱ = dlm.ˏ();
                Iterator it = ॱ.iterator();
                int i = 4;
                while (it.hasNext()) {
                    SessionDownloadTask sessionDownloadTask = (SessionDownloadTask) it.next();
                    if (sessionDownloadTask.ˈ() == 6 && sessionDownloadTask.interruptReason_ == 1) {
                        it.remove();
                    }
                    i = sessionDownloadTask.ʽॱ() | i;
                }
                if (!ॱ.isEmpty()) {
                    int ॱ2 = dvt.ॱ(ˋ);
                    for (SessionDownloadTask sessionDownloadTask2 : ॱ) {
                        if ((sessionDownloadTask2.ʽॱ() & ॱ2) > 0) {
                            sessionDownloadTask2.ʼ(false);
                            dvt.ˏ().ˎ(sessionDownloadTask2);
                        }
                    }
                    dli.ˊ(ˋ).edit().putInt("userSelectNetType", i).commit();
                } else if (VERSION.SDK_INT > 25) {
                    JobScheduler jobScheduler = (JobScheduler) ˋ.getSystemService("jobscheduler");
                    if (jobScheduler.getPendingJob(1001) != null) {
                        jobScheduler.cancel(1001);
                    }
                }
            }
        });
    }

    public static void ˋ() {
        Logger.ˎ("OOBE", "OOBEAsyncHandler.startGetIconTask");
        ˊ(new Runnable() {
            public void run() {
                Context ˋ = ContentManager.getInstant().getContext();
                if (!dld.ˎ().ᐝ().isEmpty()) {
                    for (StartupResponse$OOBEAppInfo startupResponse$OOBEAppInfo : dld.ˎ().ͺ()) {
                        if (!dof.ˋ(startupResponse$OOBEAppInfo.ʼ())) {
                            eqx.ˋ(ˋ, startupResponse$OOBEAppInfo.ʼ());
                        }
                    }
                }
            }
        });
    }

    public static void ˋ(final int i) {
        Logger.ˎ("OOBE", "OOBEAsyncHandler.startGetAppListTask");
        ˊ(new Runnable() {
            public void run() {
                dld.ˎ().ॱˎ();
                new dlv(ContentManager.getInstant().getContext(), i).ॱ();
            }
        });
    }

    private static List<SessionDownloadTask> ˏ() {
        List<SessionDownloadTask> arrayList = new ArrayList();
        List<SessionDownloadTask> ͺ = dvt.ˏ().ͺ();
        synchronized (ͺ) {
            for (SessionDownloadTask sessionDownloadTask : ͺ) {
                if (sessionDownloadTask.ˌ() == 4) {
                    arrayList.add(sessionDownloadTask);
                }
            }
        }
        return arrayList;
    }
}
